<!DOCTYPE html>
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title></title>
	<style>
		*{
			margin:0;
			padding:0;
		}
		body{
			font-family:"微软雅黑";
			background-color:#ffffff;
		}
		.layui-table-cell{
			text-align:center;
			height: auto;
			white-space: normal;
		}
</style>
	<link rel="stylesheet" href="js/plugIn/layui-master/src/css/layui.css">
	<script src="js/plugIn/jquery/jquery.min.js"></script><!--jquery 要在layui之前，这样layui就不会加载内部的jquery-->
	<script src="js/plugIn/layui-master/src/layui.js"></script>

	<script src="js/common/tool.js"></script>
	<script src="config/InitConfig.js"></script>

</head>

<body>

<form class="layui-form" action="">
	<div class="layui-form-item" style="margin: 10px;">
		<div class="layui-inline">
			<label class="layui-form-label">绑定数据点</label>
			<div class="layui-input-inline">
				<input name="hardware_input_text" readOnly=readOnly type="text" autocomplete="off" value="" id="" placeholder="点击选择数据点"  style="cursor:pointer" class="layui-input">
			</div>
		</div>

	</div>
</form>

<table class="layui-hide" id="stateList" lay-filter="stateList"></table>

	<div style="text-align:center;margin-top: 30px">
		<input type="button" onclick="okClick()" class="layui-btn" value="确定"/>
	</div>


</body>
<script type="text/html" id="operateButton">
	<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="deleteButton">删除</button>
</script>

<script>
var table_data= new Array();
var hardValue="";
if(parent.basicStateConfig==""){
	parent.basicStateConfig={};
}else{
	hardValue=parent.basicStateConfig.key;//绑定的数据点
	if(typeof(hardValue) == "undefined"||hardValue==""){
		hardValue="";
	}else{
		//$('#hardware_input_text').val(hardValue.devicename+'-'+hardValue.devicecode);
		//$('#hardware_input_text').val(hardValue.devicename+'-'+hardValue.devicecode);
		$('input[name="hardware_input_text"]').val(hardValue.devicename);
		$('input[name="hardware_input_text"]').attr("id",hardValue.devicecode);
		
	}


	var state =parent.basicStateConfig.state;// 不知道为什么直接放到表格里面不会显示 , 用下面的循环转一次才可以
	for(var i=0;i<state.length;i++){
		var whereObj_t = state[i];
		var obj_t={};
		obj_t.value=whereObj_t.value;
		obj_t.text=whereObj_t.text;
		table_data.push(obj_t);

	}


}


var stateContent;//状态类容

var index = parent.layer.getFrameIndex(window.name);

var hardware_id="";//当前选择的硬件传感器数据点的id
var hardware_Text="";//当前选择的硬件传感器数据点的描述





var table;
	layui.use(['form','element','table'], function(){
		table = layui.table;
		table.render({
			 elem: '#stateList'
			,toolbar:'<div style="float: left">状态列表<span style="font-size:12px;color:red"></span></div>'
			,defaultToolbar: [{ //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
				title: '添加条件'
				,layEvent: 'addWhere'
				,icon: 'layui-icon-add-1'
			}]
			,data:table_data
			,cellMinWidth: 80
			,even:true
			,page:false
			,limit: Number.MAX_VALUE // 数据表格默认全部显示
			,height: 'full-160'
			,cols: [[
				{field:'value', title: '状态值', align: 'center' ,edit:'text'}
				,{field:'text', title: '状态值对应的文字', align: 'center' ,edit:'text'}
				,{title: '操作',align:'center',toolbar: '#operateButton'}
			]]
		});

		//监听行工具事件
		table.on('tool(stateList)', function(obj){
			if(obj.event === 'deleteButton'){
				layer.confirm('真的删除行么', function(index){
					var trElem = obj.tr;
					var tableView = trElem.closest(".layui-table-view");
					var t_data = table.cache[tableView.attr("lay-id")];
					if (obj.tr.data('index') >= 0) {
						t_data.splice(obj.tr.data('index'), 1)//根据索引删除当前行
						table.reload(tableView.attr("lay-id"), {data:t_data});
					}



					layer.close(index);
				});
			}
		});
		//头工具栏事件
		table.on('toolbar(stateList)', function(obj){
			switch(obj.event){
				case 'addWhere':

					var tjzt_data =table.cache["stateList"];
					tjzt_data.push({
						"value": ""
						,"text": ""
					});
					table.reload('stateList', {data:tjzt_data});
					break;
			};





		});



        //选择数据点
		$('input[name="hardware_input_text"]').on('click', function(){
			// debugger;
			var current_input = $(this);
			//var input_value = $(this).val();// 当前文本框的值
			var input_value = $(this).attr("id"); // 当前文本框的值转移到ID上
			layer.open({
				title:'请选择需要绑定的数据点',
				type: 2,
				area: ['452px', '584px'],
				fixed: false, //不固定
				maxmin: true,
				content: 'hardwareSelect.html?hardware_id='+input_value,
				end: function(index, layero){
					//   var hardware_id="";//当前选择的硬件传感器数据点的id
					//    var hardware_Text="";//当前选择的硬件传感器数据点的描述

					if(hardware_id!==''){
						current_input.val(hardware_Text);
						hardValue = {devicecode:hardware_id,devicename:hardware_Text};

						hardware_id="";
						hardware_Text="";
					}

				}

			});


		});





	});

//点击确认按钮
function okClick(){
//debugger;
	parent.basicStateConfig.key=hardValue;
	stateContent=table.cache['stateList'];//表格数据
	parent.basicStateConfig.state=stateContent;
	parent.layer.close(index);
}
</script>
</html>

